<?php
class RegistrationsController extends AppController {

	var $name = 'Registrations';
	var $uses = array('User', 'School', 'Group', 'Contact', 'Subscriber', 'Group');

	function beforeFilter() {
		//		parent::beforeFilter();
		$this->layout = 'subscriber';
	}

	function add() {
		$sid = $this->params['pass'][0];
		$listGroups = $this->Group->getListGroupId($sid);
		$this->set('listGroups',$listGroups);
	}

	function review() {
		// debug($this->data);
		$this->set('data',$this->data);
	}

	function registration() {
		//		debug($this->data);

		$this->data['User']['type_user_id'] = 3;

		if (!empty($this->data)) {
//			$this->Subscriber->create();
//			if ($this->Subscriber->saveAll($this->data)) {
//				
//			} else {
//				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
//			}
			
			if ($this->Subscriber->saveAll($this->data, array('validate' => 'only'))) {
				$this->User->create();
				
				$this->User->begin();
				if ($this->User->save($this->data)) {
					
					if ($this->User->Contact->save($this->data)) {
						$this->Session->setFlash(__('The User has been saved', true));
						//$this->redirect(array('controller'=>'schools','action'=>'index'));
					} else {
						$this->User->rollback();
						$this->Session->setFlash(__('The Contact could not be saved. Please, try again.', true));
					}
					
				} else {
					$this->User->rollback();
					$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
				}
			} 
			else {
				debug($this->data);
			}
		}

		$this->set('data',$this->data);
		// Render the element in /views/elements/ajaxreturn.ctp
		$this->render('/registrations/review');
	}



}
?>